/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.Articolo;
import ejb.BusinessLogic;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Max
 */
public class CreaNegozio extends HttpServlet {

    @EJB
    private BusinessLogic businessLogic;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int quant = 0;

        List<Articolo> a2;
        a2 = businessLogic.listaArticoliOrdinati();
        int i = 0;
        String cerca = request.getParameter("search");
        String foto = "";
        String marcaAttuale = "";
        String modelloAttuale = "";
        if (cerca == null) {
            while (i < a2.size()) {
                boolean flag = true;

                String filtro = request.getParameter("filtra");
                String marcaFiltro = request.getParameter("marca");
                String marcaProd = a2.get(i).getMarca();
                String tipoFiltro = request.getParameter("tipo");
                String tipoProd = a2.get(i).getTipo();
                String modelloProd = a2.get(i).getModello();
                String tagliaFiltro = request.getParameter("taglia");
                String tagliaProd = a2.get(i).getTaglia();
                quant = a2.get(i).getQuantita();
                float prezzo = a2.get(i).getPrezzo_vendita();
                String articolo = "<a href='prodotto.jsp?valore=" + a2.get(i).getId() + "'>";
                if (filtro != null) {
                    if (!(marcaFiltro.equals("")) && !(marcaFiltro.equals(marcaProd))) {
                        flag = false;
                    }
                    if (!(tipoFiltro.equals("")) && !(tipoFiltro.equals(tipoProd))) {
                        flag = false;
                    }
                    if ((tagliaFiltro != null) && !(tagliaFiltro.equals("")) && !(tagliaFiltro.equals(tagliaProd))) {
                        flag = false;
                    }
                }
                if (flag && quant>0) {
                    if (!(marcaAttuale.equals(marcaProd)) || !(modelloAttuale.equals(modelloProd))) {
                        marcaAttuale = marcaProd;
                        modelloAttuale = modelloProd;
                        foto = a2.get(i).getFoto();
                        out.println("<div class='prodotto'>");
                        out.println(articolo + "<img src='images/" + foto + "' alt='' width='60' height='100' class='left' /></a><div class='marca'>" + marcaProd + "</div>");
                        out.println("<div class='modello'>" + modelloProd + "</div>");
                        out.println("<div class='prezzo'>" + prezzo + " €</div>");
                        out.println("</div>");
                    }
                }
                i++;
            }
        } else {
            int ric = 0;
            boolean flag1 = false, flag2 = false, ok = false;
            while ((i < a2.size()) && !flag1 && ric < 3) {
                float prezzo = a2.get(i).getPrezzo_vendita();
                quant = a2.get(i).getQuantita();
                if (ric == 0) {
                    if (cerca.equalsIgnoreCase(a2.get(i).getModello())) {
                        ok = true;
                        flag2 = true;
                    }
                } else if (ric == 1) {
                    if (cerca.equalsIgnoreCase(a2.get(i).getMarca())) {
                        ok = true;
                        flag2 = true;
                    }
                } else {
                    if (cerca.equalsIgnoreCase(a2.get(i).getTipo())) {
                        ok = true;
                        flag2 = true;
                    }
                }
                if (ok && quant>0) {
                    String marcaProd = a2.get(i).getMarca();
                    String modelloProd = a2.get(i).getModello();
                    if (!(marcaAttuale.equals(marcaProd)) || !(modelloAttuale.equals(modelloProd))) {
                        marcaAttuale = marcaProd;
                        modelloAttuale = modelloProd;
                        foto = a2.get(i).getFoto();
                        String articolo = "<a href='prodotto.jsp?valore=" + a2.get(i).getId() + "'>";
                        out.println("<div class='prodotto'>");
                        out.println(articolo + "<img src='images/" + foto + "' alt='' width='60' height='100' class='left' /></a><div class='marca'>" + a2.get(i).getMarca() + "</div>");
                        out.println("<div class='modello'>" + a2.get(i).getModello() + "</div>");
                        out.println("<div class='prezzo'>" + prezzo + " €</div>");
                        out.println("</div>");
                    }
                }
                i++;
                ok = false;
                if ((i == a2.size())) {
                    if (flag2) {
                        flag1 = true;
                    } else {
                        i = 0;
                        ric++;
                    }
                }
            }
            if (!flag1) {
                out.println("<div>Nessun prodotto trovato</div>");
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
